package com.louis.ajc.properties;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PropertyPage;

public class AjcPropertyPage extends PropertyPage {

	private static final String APSECTJ_PATH = "&Spring aspect name:";
	private static final String Aspectpath_PROPERTY = "Spring_aspect_name";
	private static final String DEFAULT_VALUE = "spring-aspects";
	private static final int TEXT_FIELD_WIDTH = 50;

	private Text apsectpathText;

	/**
	 * Constructor for SamplePropertyPage.
	 */
	public AjcPropertyPage() {
		super();
	}

	private void addFirstSection(Composite parent) {
		createDefaultComposite(parent);
	}

	private void addSeparator(Composite parent) {
		Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		separator.setLayoutData(gridData);
	}

	private void addSecondSection(Composite parent) {
		Composite composite = createDefaultComposite(parent);

		// Label for owner field
		Label aspectpathLabel = new Label(composite, SWT.NONE);
		aspectpathLabel.setText(APSECTJ_PATH);

		// Owner text field
		apsectpathText = new Text(composite, SWT.SINGLE | SWT.BORDER);
		GridData gd = new GridData();
		gd.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
		apsectpathText.setLayoutData(gd);

		// Populate owner text field
		try {
			String owner = ((IResource) getElement())
					.getPersistentProperty(new QualifiedName("",
							Aspectpath_PROPERTY));
			apsectpathText.setText((owner != null) ? owner : DEFAULT_VALUE);
		} catch (CoreException e) {
			apsectpathText.setText(DEFAULT_VALUE);
		}
	}

	/**
	 * @see PreferencePage#createContents(Composite)
	 */
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		GridData data = new GridData(GridData.FILL);
		data.grabExcessHorizontalSpace = true;
		composite.setLayoutData(data);

		addFirstSection(composite);
		addSeparator(composite);
		addSecondSection(composite);
		return composite;
	}

	private Composite createDefaultComposite(Composite parent) {
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		composite.setLayout(layout);

		GridData data = new GridData();
		data.verticalAlignment = GridData.FILL;
		data.horizontalAlignment = GridData.FILL;
		composite.setLayoutData(data);
		return composite;
	}

	protected void performDefaults() {
		super.performDefaults();
		// Populate the owner text field with the default value
		apsectpathText.setText("");
	}

	public boolean performOk() {
		// store the value in the owner text field
		try {
			((IResource) getElement()).setPersistentProperty(new QualifiedName(
					"", Aspectpath_PROPERTY), apsectpathText.getText());
		} catch (CoreException e) {
			return false;
		}
		return true;
	}

}